package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.adapter.HomeViewPagerAdapter;
import com.example.myapplication.frament.FragmentDynasty;
import com.example.myapplication.frament.FragmentHome;
import com.example.myapplication.frament.FragmentMe;
import com.example.myapplication.frament.FragmentNation;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private ImageView mImageView;

    @BindView(R.id.HomeViewPager)
    ViewPager HomeViewPager;
    @BindView(R.id.main_radiobutton1)
    RadioButton mainRadiobutton1;
    @BindView(R.id.main_radiobutton2)
    RadioButton mainRadiobutton2;
    @BindView(R.id.main_radiobutton3)
    RadioButton mainRadiobutton3;
    @BindView(R.id.main_radiobutton4)
    RadioButton mainRadiobutton4;
    @BindView(R.id.main_radiogroup)
    RadioGroup mainRadiogroup;
    private List<RadioButton> buttonList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setViewPager();
        setButton();
    }

    private void setButton() {
        buttonList = new ArrayList<>();
        buttonList.add(mainRadiobutton1);
        buttonList.add(mainRadiobutton2);
        buttonList.add(mainRadiobutton3);
        buttonList.add(mainRadiobutton4);
    }

    private void setViewPager() {
        List<Fragment> list = new ArrayList<>();
        list.add(new FragmentDynasty());
        list.add(new FragmentHome());
        list.add(new FragmentNation());
        list.add(new FragmentMe());
        HomeViewPagerAdapter adapter = new HomeViewPagerAdapter(getSupportFragmentManager() , list);
        HomeViewPager.setAdapter(adapter);
        HomeViewPager.addOnPageChangeListener(new pagerChangeListener());
    }

    @OnClick({R.id.main_radiobutton1, R.id.main_radiobutton2, R.id.main_radiobutton3, R.id.main_radiobutton4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_radiobutton1:
                HomeViewPager.setCurrentItem(0);
                break;
            case R.id.main_radiobutton2:
                HomeViewPager.setCurrentItem(1);
                break;
            case R.id.main_radiobutton3:
                HomeViewPager.setCurrentItem(2);
                break;
            case R.id.main_radiobutton4:
                HomeViewPager.setCurrentItem(3);
                break;
        }
    }

    private class pagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            select(buttonList.get(position));
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void select(RadioButton button) {
        mainRadiobutton1.setSelected(false);
        mainRadiobutton2.setSelected(false);
        mainRadiobutton3.setSelected(false);
        mainRadiobutton4.setSelected(false);
        button.setChecked(true);
    }
}